package cn.yshujia.handler;

import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author YShuJia
 * @create 2024/12/16
 * @description SocketIOHandler
 */

@Slf4j
@Component
public class SocketIOHandler {
	
	
	private final Map<String, Method> eventMethods = new ConcurrentHashMap<>();
	
	@OnConnect
	public void onConnect(SocketIOClient client) {
		String userId = client.getHandshakeData().getSingleUrlParam("userId");
		UUID sessionId = client.getSessionId();
//		socketUtils.saveClient(userId, sessionId, client);
		System.out.println("userId: " + userId + "连接建立成功 - " + sessionId);
	}
	
	
	/**
	 * @param client: [client] * @return void
	 * @author YShuJia
	 * @description 客户端关闭连接时触发：前端触发：socket.disconnect();
	 * @create 2024/12/16 20:59
	 */
	@OnDisconnect
	public void onDisconnect(SocketIOClient client) {
		String userId = client.getHandshakeData().getSingleUrlParam("userId");
		UUID sessionId = client.getSessionId();
//		socketUtils.deleteSessionClientByUserId(userId, sessionId);
		System.out.println("userId: " + userId + "连接关闭成功 - " + sessionId);
	}
}
